from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QListWidget, QListWidgetItem, QLabel

from common.equipment.reference import Equipment
from common.player.reference import Player
from gui.signals import SIGNALS


class DialogListEquip(QWidget):
    def __init__(self, data, player: Player):
        super().__init__()

        self.player = player
        self.data = data

        self.setWindowTitle('可用装备列表')

        self.list = QListWidget(self)
        self.list.setStyleSheet("QListWidget::item{height: 30px}")
        self.list.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        self.list.itemClicked.connect(self.selected)

        self.set_data()

    def set_data(self):
        self.list.clear()
        for equip in self.data:
            item = QListWidgetItem()
            item.setData(Qt.ItemDataRole.UserRole, equip)
            self.list.addItem(item)
            label = QLabel(equip.overview())
            label.setFixedHeight(30)
            self.list.setItemWidget(item, label)

    def selected(self, item):
        equip: Equipment = item.data(Qt.ItemDataRole.UserRole)
        equip.install(self.player)
        SIGNALS.update_player_penal.emit()
        SIGNALS.update_equip_table.emit()
        self.close()

